﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SARUserControls
{
    /// <summary>
    /// 进度条控件
    /// </summary>
    public partial class SARProgressBar : UserControl
    {
        public SARProgressBar()
        {
            this.InitializeComponent();
        }

        private double progressValue = 0;

        /// <summary>
        /// 进度值
        /// </summary>
        public double SARProgressValue
        {
            get { return progressValue; }
            set
            {
                if (value < 0)
                {
                    value = 0;
                }
                else if (value > 1)
                {
                    value = 1;
                }
                progressValue = value;

                gridProgress.Width = 544.5 * value + 9;
                pathProgress1.Width = 544.5 * value + 9;
                pathProgress2.Width = 544.5 * value + 8;

                gridBall.Margin = new Thickness(544.5 * value - 38, 0, 0, 0);
                RotateTransform rotateTransform = new RotateTransform((544.5 * value) / (122 * 3.1415926) * 360);
                gridBall.RenderTransform = rotateTransform;
            }
        }

        /// <summary>
        /// 开始按钮单击事件
        /// </summary>
        public event MouseButtonEventHandler SARStartClick;

        /// <summary>
        /// 停止按钮单击事件
        /// </summary>
        public event MouseButtonEventHandler SARStopClick;

        private void gridStartMouse_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!startEnabled)
            {
                return;
            }
            this.pathPlay.Margin = new Thickness(46.5, 6.5, 0, 0);
            if (SARStartClick != null)
            {
                SARStartClick.Invoke(this, e);
            }
        }

        private void gridStartMouse_MouseLeave(object sender, MouseEventArgs e)
        {
            if (!startEnabled)
            {
                return;
            }
            this.pathPlay.Margin = new Thickness(44.5, 4.5, 0, 0);
        }

        private void gridStartMouse_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (!startEnabled)
            {
                return;
            }
            this.pathPlay.Margin = new Thickness(44.5, 4.5, 0, 0);
        }

        private void gridEndMouse_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!stopEnabled)
            {
                return;
            }
            this.pathStop.Margin = new Thickness(198.5, 11.5, 42.5, 11.5);
            if (SARStopClick != null)
            {
                SARStopClick.Invoke(this, e);
            }
        }

        private void gridEndMouse_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (!stopEnabled)
            {
                return;
            }
            this.pathStop.Margin = new Thickness(196.5, 9.5, 44.5, 13.5);
        }

        private void gridEndMouse_MouseLeave(object sender, MouseEventArgs e)
        {
            if (!stopEnabled)
            {
                return;
            }
            this.pathStop.Margin = new Thickness(196.5, 9.5, 44.5, 13.5);
        }

        private bool startEnabled = true;

        /// <summary>
        /// 开始按钮是否处于启用状态
        /// </summary>
        public bool SARStartEnabled
        {
            get { return startEnabled; }
            set 
            { 
                startEnabled = value;
                if (value)
                {
                    this.gridStart.Opacity = 1;
                    this.pathPlay.Opacity = 1;
                }
                else
                {
                    this.gridStart.Opacity = 0.5;
                    this.pathPlay.Opacity = 0.5;
                }
            }
        }
        private bool stopEnabled = true;

        /// <summary>
        /// 停止按钮是否处于启用状态
        /// </summary>
        public bool SARStopEnabled
        {
            get { return stopEnabled; }
            set
            {
                stopEnabled = value;
                if (value)
                {
                    this.gridStop.Opacity = 0.5;
                    this.pathStop.Opacity = 0.5;
                }
                else
                {
                    this.gridStop.Opacity = 1;
                    this.pathStop.Opacity = 1;
                }
            }
        }
    }
}